<?php
/**
 * User: Jerry
 * Date: 2017/9/23
 * Time: 10:07
 */

namespace SDXApi\Service;


use SDXApi\Model\CollectGoodsModel;
use SDXApi\Model\FavourableActivityModel;
use SDXApi\Model\NewCartModel;
use SDXApi\Model\UsersModel;
use SDXApi\Repositories\GoodsRepository;

/**
 * Class CollectService
 * @author Jerry
 * @package SDXApi\Service
 */
class CollectService
{
    /**
     * @var mixed
     */
    protected $pageSize;

    /**
     * @var CollectGoodsModel
     */
    protected $collectModel;

    /**
     * @var UsersModel
     */
    protected $userModel;

    /**
     * @var FavourableActivityModel
     */
    protected $favourableActivityModel;

    protected $goodsRepository;

    protected $newCartModel;

    /**
     * 注入依赖
     * CollectService constructor.
     */
    public function __construct()
    {
        $this->pageSize = C("page_size");
        $this->collectModel = new CollectGoodsModel();
        $this->userModel = new UsersModel();
        $this->favourableActivityModel = new FavourableActivityModel();
        $this->goodsRepository = new GoodsRepository();
        $this->newCartModel = new NewCartModel();
    }

    /**
     * 获取收藏数据
     * @author Jerry
     * @param int $userId
     * @param int $wid
     * @param int $page
     * @return array
     */
    public function getCollectList($userId = 0, $wid = 8, $page = 1)
    {
        $collectGoods = $this->findCollectGoods($userId, $wid);

//        $favourableList = $this->getFavourableList($userId);
        $favourableList = $this->newCartModel->favourable_list_flow($userId, $wid);

        if (! empty($collectGoods)) {
            $this->goodsRepository->wid = $wid;
            $this->goodsRepository->userId = $userId;
            $collectGoods = $this->goodsRepository->formatGoodsTransform($collectGoods);
        }

        $data = $this->handleFavourableData($favourableList, $collectGoods);

        return $data;
    }

    /**
     * @author Jerry
     * @param array $favourableList
     * @param array $goodsData
     * @return array
     */
    protected function handleFavourableData($favourableList = array(), $goodsData = array())
    {
        if (empty($favourableList)) {
            $favourableList = array();
        }

        if (empty($goodsData)) {
            $goodsData = array();
        }

        $data = array();

        foreach ($goodsData as $key => $value) {

            if ($value['goods_number'] == 0) {
                continue;
            }

            $favourableData = $this->getGoodsFavourableList($favourableList, $value);

            if (empty($favourableData) && $value['promote_price'] == 0) {
                continue;
            }

            $value['favourable_list'] = $favourableData;
            $data[] = $value;
        }

        $result['data'] = $data;

        return $result;
    }

    /**
     * @author Jerry
     * @param array $favourable_list
     * @param array $goods
     * @return array
     */
    public function getGoodsFavourableList($favourable_list = array(), $goods = array())
    {
        $goods_id = $goods['goods_id'];
        $goods_cate = $goods['cat_id'];
        $goods_brand= $goods['brand_id'];
        $goods['promote_start_date'] = local_strtotime($goods['promote_start_date']);
        $goods['promote_end_date'] = local_strtotime($goods['promote_end_date']);
        //促销价范围处理
        $promoteService = new PromoteService();
        $is_promote = $promoteService->getPromotePriceStatus($goods);
        //商品可参与的活动列表
        $goods_activity_list = array();

        foreach ($favourable_list as $f_key => $f_avl) {
            //判断促销商品是否可以参加活动
            $goods_allow_join = false;
            if($is_promote===true){
                $act_promote_ext = explode(',',$f_avl['act_promote_ext']);
                if($f_avl['act_promote_goods_type']==1){
                    $goods_allow_join = true;
                }elseif($f_avl['act_promote_goods_type']==2 && in_array($goods_id,$act_promote_ext)){
                    $goods_allow_join = true;
                }
            }else{
                $goods_allow_join = true;
            }
            if ($goods_allow_join !== true) {
                continue;
            }

            //普通商品以及可以参与活动的促销商品，获取商品的活动信息
            $act_range_ext = explode(',', $f_avl['act_range_ext']);
            //如果优惠范围为分类且设置包含子分类，则获取当前设置分类的所有子分类
            if($f_avl['act_range'] == 1 && $f_avl['is_include_subcategory'] == 1){
                $act_range_ext = $this->newCartModel->get_sub_cate($act_range_ext);
            }
            if ($f_avl['act_range'] == 0 || ($f_avl['act_range'] == 1 && in_array($goods_cate, $act_range_ext)) || ($f_avl['act_range'] == 2 && in_array($goods_brand, $act_range_ext)) || ($f_avl['act_range'] == 3 && in_array($goods_id, $act_range_ext))) {
                //商品可参与的活动
                $goods_activity = array();
                $goods_activity['act_id'] = $f_avl['act_id'];
                $goods_activity['act_name'] =$f_avl['act_name'];
                $goods_activity['act_tag'] = $f_avl['act_tag'];
                $goods_activity['act_gift'] = $f_avl['gift'];
                $goods_activity_list[] = $goods_activity;
            }

        }

        return $goods_activity_list;
    }

    /**
     * @author Jerry
     * @param int $userId
     * @param int $wid
     * @return mixed
     */
    protected function findCollectGoods($userId = 0, $wid = 8)
    {
        return $this->collectModel->findCollectGoodsByUserId($userId, $wid);
    }

    /**
     * @author Jerry
     * @param int $userId
     * @return mixed
     */
    protected function getFavourableList($userId = 0)
    {
        $userRank = $this->getUserRank($userId);

        $data = $this->favourableActivityModel->findFavourableListByUserRank($userRank);

        if (! empty($data)) {
            foreach ($data as $key => $value) {
                $data[$key]['gift'] = unserialize($value['gift']);
                $data[$key]['quota'] = unserialize($value['quota']);
            }
        }

        return $data;
    }

    /**
     * @author Jerry
     * @param int $userId
     * @return int
     */
    protected function getUserRank($userId = 0)
    {
        $user = $this->userModel->get_user_rank($userId);

        if (empty($user)) {
            return 1;
        }

        return $user['user_rank'];
    }
}